package com.sykj.manage.web;

import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Size;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.fabric.xmlrpc.base.Data;
import com.sykj.manage.service.BisShopService;
import com.sykj.manage.model.BisShop;
import com.sykj.util.MYSConstant;
import com.sykj.util.ManageBody;
import com.sykj.util.PageUtil;
/**
 * 商家信息表控制类
 * @author 蔡聪
 *	2018年9月10日下午3:17:40
 */
@Controller
@RequestMapping("/bisshop")
public class BisShopController {
	
	/*static final String UPLOAD_PATH = "/static/manage/images/";*/
	/**
	 * 调用商家信息业务层类
	 */
	@Autowired
	BisShopService bisshopservice;
	/**
	 * 显示全部商家信息实现方法
	 * @param model
	 * @return
	 */
	@RequestMapping("/")
	@ResponseBody
	public ManageBody bisshopShow(BisShop bisShop,Integer offset,Integer pageSize){
		PageHelper.startPage(offset==null?1:PageUtil.getPageNum(offset, pageSize),pageSize==null?10:pageSize,true);
		List<BisShop> allBisShop = bisshopservice.getAllBisShop(bisShop);
		PageInfo<BisShop>pageInfo=new PageInfo<BisShop>(allBisShop);
		return ManageBody.getData((int)pageInfo.getTotal(),pageInfo.getList(),"");
	}
	
	/**
	 * 根据id来删除商家信息
	 * @param shop_id
	 * @return
	 */
	@RequestMapping("/del/{shop_id}")
	@ResponseBody
	public ManageBody del(@PathVariable("shop_id")String shop_id){
		int deleBisShop = bisshopservice.deleBisShop(shop_id);
		if (deleBisShop>0) {
			return ManageBody.getData(1, deleBisShop, "");
		}else {
			return ManageBody.getData(1, deleBisShop, "");
		}
		
		
	}
	
	/**
	 * 新增店铺信息
	 * @param bisShop
	 * @return
	 * @throws IOException 
	 * 
	*/
	@RequestMapping("/add")
	public String add( BisShop bisShop,/*@RequestParam("file")*/ @RequestParam(value="file",required=false) MultipartFile[] file, HttpServletRequest request) throws IOException {
		int count=1;
		for(MultipartFile mf:file){
			if (!mf.isEmpty()) {
				//使用uuid给图片重命名，并去掉四个“-”
				String name = UUID.randomUUID().toString().replaceAll("-", "");
				//获取文件的扩展名
				String ext = FilenameUtils.getExtension(mf.getOriginalFilename());
				String url = request.getSession().getServletContext().getRealPath("/");
				System.out.println(url);
				String fullpath = MYSConstant.UPLOAD_MANAGE_PROJECT+name+"."+ext;
				File file2=new File(url+fullpath);
				System.out.println("````````````````````````"+file2);
				if (!file2.exists()) {
					file2.createNewFile();
				}
				mf.transferTo(file2);
				if (count==1) {
					bisShop.setShop_detail_image(fullpath);
				}else if (count==2) {
					bisShop.setShop_image(fullpath);
				}
			}
			count++;
		}
		
		
		/*String createuserid=bisShop.getCreate_user_id();
     	bisShop.setCreate_user_id(DigestUtils.md5Hex(createuserid));
		Date date=new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhhmmss");
		bisShop.setCreate_user_id(format.format(date));*/
		bisShop.setCreate_time(new Date());
     	System.out.println("===="+bisShop);
     	 bisshopservice.insertBisShop(bisShop);
 		return "redirect:/manage/shangjialiebiao.html";
		 
	}
	
	/**
	 * 根据id来查一条数据
	 * @param shop_id
	 * @return
	 */
	@RequestMapping("/updateAll")
	@ResponseBody
	public Object updateAll(String shop_id){
		
		BisShop updateAll = bisshopservice.updateAll(shop_id);
		return updateAll;
	}
	
	
	/**
	 * 修改商铺信息
	 * @param bisShop
	 * @return
	 */
	@RequestMapping("/updateBisShop")
	public String updateBisShop(BisShop bisShop,@RequestParam(value="files",required=false) MultipartFile[] file, HttpServletRequest request)throws IOException{
		int count=1;
		for(MultipartFile mf:file){
			if (!mf.isEmpty()) {
				//使用uuid给图片重命名，并去掉四个“-”
				String name = UUID.randomUUID().toString().replaceAll("-", "");
				//获取文件的扩展名
				String ext = FilenameUtils.getExtension(mf.getOriginalFilename());
				String url = request.getSession().getServletContext().getRealPath("/");
				System.out.println(url);
				String fullpath = MYSConstant.UPLOAD_MANAGE_PROJECT+name+"."+ext;
				File file2=new File(url+fullpath);
				if (!file2.exists()) {
					file2.createNewFile();
				}
				mf.transferTo(file2);
				if (count==1) {
					bisShop.setShop_detail_image(fullpath);
				}else if (count==2) {
					bisShop.setShop_image(fullpath);
				}
			}
			count++;
		}
		
		bisShop.setModify_time(new Date());
		 bisshopservice.updateBisShop(bisShop);
		return "redirect:/manage/shangjialiebiao.html";
		
	}
	
	
}
